import 'dart:io';
import 'dart:async';

import 'package:flutter/material.dart';

import 'package:path_provider/path_provider.dart';
import './screenMode/main.dart';

class ScreenPage extends StatefulWidget {
  Map<String, dynamic> screenSetting;
  ScreenPage({Key key, this.screenSetting}) : super(key: key);

  @override
  _ScreenPageState createState() => new _ScreenPageState();
}

class _ScreenPageState extends State<ScreenPage> {
  String _materialDirPath;
  bool _isLoading = false;
  String _mode;
  /// 分屏方案id
  String _id;
  List<Map<String, dynamic>> _blocks;
  @override
  void initState() {
    super.initState();
    // _isLoading = true;
    _prepare();
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: Builder(
        builder: (context) => _isLoading
          ? new Center(
              child: new CircularProgressIndicator(),
            )
          : _blocks == null
            ?  Container()
            : _buildPage(_mode, _blocks),
      ),
    );
  }
  @override
  void dispose() {
    print('screen_page dispose');
    super.dispose();
  }

  Widget _buildPage(String screenMode, List blocks) {
    switch (screenMode) {
      case 'fullScreen': {
        return FullScreen(materialPath: _materialDirPath, blocks: blocks);
      }
      case 'twoScreen': {
        return TwoScreen(materialPath: _materialDirPath, blocks: blocks);
      }
      case 'threeScreen50': {
        return ThreeScreen50(materialPath: _materialDirPath, blocks: blocks);
      }
      case 'threeScreen66': {
        return ThreeScreen66(materialPath: _materialDirPath, blocks: blocks);
      }
      case 'fourScreenVertial': {
        return FourScreenVertial(materialPath: _materialDirPath, blocks: blocks);
      }
      case 'fourScreenSquare': {
        return FourScreenSquare(materialPath: _materialDirPath, blocks: blocks);
      }

      default: {
        return FullScreen(materialPath: _materialDirPath, blocks: blocks);
      }
    }
  }

  /// 初始化
  Future<Null> _prepare() async {
    // dirPath = (await getApplicationDocumentsDirectory()).path + '/player_material/$_id';
    final dirPath = (await getExternalStorageDirectory()).path + '/player_material/${widget.screenSetting['id']}';
    setState(() {
      _id = widget.screenSetting['id'];
      _materialDirPath = dirPath;
      _mode = widget.screenSetting['mode'];
      _blocks = widget.screenSetting['blocks'];
      _isLoading = false;
    });
  }
}